package com.atguigu.ucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.nacosclass.service_ucenter.UcenterOrderVO;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.dto.UserLoginMemberDTO;
import com.atguigu.ucenter.entity.dto.UserRegisterMemberDTO;
import com.atguigu.ucenter.entity.vo.UserCommentInfoVO;
import com.atguigu.ucenter.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
@Slf4j
public class MemberController {
    @Autowired
    MemberService memberService;

    //登录接口
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public Result login(@RequestBody UserLoginMemberDTO userLoginMemberDTO) {
        log.info("我是谁" + userLoginMemberDTO);
        //调用memberService方法实现登录功能
        String token = memberService.login(userLoginMemberDTO);
        return Result.ok().data("token",token);
    }
    //注册接口
    @PostMapping("register")
    public Result register(@RequestBody UserRegisterMemberDTO userRegisterMemberDTO) {
        memberService.register(userRegisterMemberDTO);
        return Result.ok();
    }

    //根据token获取用户信息
    @RequestMapping(value = "getMemberInfo", method = RequestMethod.GET)
    public Result getMemberInfo(HttpServletRequest request) {
        //调用jwt工具的方法，根据request对象获取头信息。返回用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        Member byId = memberService.getById(memberIdByJwtToken);
        log.info("我为啥没出来呢：" + byId);
        return Result.ok().data("byId",byId);
    }

    //根据用户id获取评论指定相应信息
    @GetMapping("getOpenIdMember/{userId}")
    public UserCommentInfoVO getOpenIdMember(@PathVariable String userId) {
        log.info("我是 userId:{}",userId);

        Member member = memberService.getById(userId);
        UserCommentInfoVO userCommentInfoVO = new UserCommentInfoVO();
        BeanUtils.copyProperties(member,userCommentInfoVO);
        log.info("我是userCommentInfoVO:{}",userCommentInfoVO);
        return userCommentInfoVO;
    }
    //提供订单相关的信息
    @GetMapping("getOrderIdMember/{userId}")
    public UcenterOrderVO getOrderIdMember(@PathVariable String userId) {
        Member member = memberService.getById(userId);
        UcenterOrderVO ucenterOrderVO = new UcenterOrderVO();
        BeanUtils.copyProperties(member,ucenterOrderVO);
        return ucenterOrderVO;
    }
    //查询某一天注册的人数
    @GetMapping("countRegister/{day}")
    public Result countRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return Result.ok().data("countRegister",count);
    }
}

